package com.beiye.drivertransport.illegallearningfragment;


import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.MotionEvent;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.SubActivity.IllegallearningCourseDetailsActivity;
import com.beiye.drivertransport.SubActivity.SpecifiedLearningCourseDetailsAgainActivity;
import com.beiye.drivertransport.adapter.AdditionCourseApt;
import com.beiye.drivertransport.bean.AddittionCourseBean;
import com.beiye.drivertransport.fragment.TwoBaseFgt;
import com.beiye.drivertransport.http.Login;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.beiye.drivertransport.utils.Utils;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;


import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
//附加课程
  public class AdditionalCoursesFragment extends TwoBaseFgt {
  private int firstIndex=1;
  private int pageSize=10;

  View empty_view;

  LRecyclerView lv_additioncourse;
  private LRecyclerViewAdapter lRecyclerViewAdapter1;
  private AdditionCourseApt additionCourseApt;


  @Override
  public int getLayoutId() {
    return R.layout.fragment_additional_courses;

  }


  public void onClick(View view) {

  }

  @Override
  public void initData() {
    empty_view = (View) getActivity().findViewById(R.id.empty_view);
    lv_additioncourse = (LRecyclerView) getActivity().findViewById(R.id.lv_additioncourse);

    if (getArguments() != null) {
      int uvtSn = getArguments().getInt("uvtSn");//培训系列号
     // int mark = getArguments().getInt("mark");
      SharedPreferences sp = getActivity().getSharedPreferences("uvtSnandmark1",
              Context.MODE_PRIVATE);
      SharedPreferences.Editor edit = sp.edit();
      edit.putInt("uvtSn",uvtSn);
     // edit.putInt("mark",mark);
      edit.commit();
    }
    initUi();
  }

  private void initUi() {
    LinearLayoutManager ms = new LinearLayoutManager(getActivity());
    ms.setOrientation(LinearLayoutManager.VERTICAL);
    lv_additioncourse.setLayoutManager(ms);
    additionCourseApt = new AdditionCourseApt(getContext());
    lRecyclerViewAdapter1 = new LRecyclerViewAdapter(additionCourseApt);
    lv_additioncourse.setAdapter(lRecyclerViewAdapter1);
    lv_additioncourse.setFooterViewColor(R.color.colorAccent, R.color.dark ,android.R.color.white);
    lv_additioncourse.setFooterViewHint("拼命加载中","已经全部为你呈现了","网络不给力啊，点击再试一次吧");
    lv_additioncourse.setOnRefreshListener(new OnRefreshListener() {

      @Override
      public void onRefresh() {
        firstIndex = 1;
        requestData();
      }
    });

    lv_additioncourse.setOnLoadMoreListener(new OnLoadMoreListener() {

      @Override
      public void onLoadMore() {
        firstIndex+=pageSize;
        requestData();
      }
    });
    empty_view.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        lv_additioncourse.refresh();
      }
    });

    lRecyclerViewAdapter1.setOnItemClickListener(new com.github.jdsjlzx.interfaces.OnItemClickListener() {
      @Override
      public void onItemClick(View view, int position) {
        if (Utils.isFastDoubleClick()) {
          return ;
        }
        try {
          //校验是否学过课程
          int tcSn = additionCourseApt.getDataList().get(position).getTcSn();
          int sn = additionCourseApt.getDataList().get(position).getSn();
          int finishMark = additionCourseApt.getDataList().get(position).getFinishMark();
          int havedLength = additionCourseApt.getDataList().get(position).getHavedLength();
          if(finishMark==0)
          {
            showLoadingDialog("正在加载");
            Bundle bundle = new Bundle();
            bundle.putInt("tcsn", tcSn);
            bundle.putInt("sn", sn);
            bundle.putInt("minPer", 100);
            bundle.putInt("havedLength",havedLength);
            startActivity(IllegallearningCourseDetailsActivity.class, bundle);
          }else if(finishMark==1){
            TiShiDialog.Builder builder1 = new TiShiDialog.Builder(getActivity());
            builder1.setMessage("您已学习了该课程，继续浏览课程将不记录时间");
            builder1.setTitle("提示:");
            builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                int tcSn = additionCourseApt.getDataList().get(position).getTcSn();
                Bundle bundle = new Bundle();
                bundle.putInt("tcsn", tcSn);
                startActivity(SpecifiedLearningCourseDetailsAgainActivity.class, bundle);
                //设置你的操作事项
              }
            });
            builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();

              }
            });
            builder1.create().show();

          }


        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
    lv_additioncourse.refresh();
  }

  @Override
  public void requestData() {
    SharedPreferences sp = getActivity().getSharedPreferences("uvtSnandmark1",
            Context.MODE_PRIVATE);
    int uvtSn = sp.getInt("uvtSn", 0);
    new Login().getIllegalAdditionData(uvtSn,firstIndex,pageSize,this, 1);

  }

  @Override
  public void onSuccess(String result, Call call, Response response, int what) {
    if(what==1)
    {
      AddittionCourseBean addittionCourseBean = JSON.parseObject(result, AddittionCourseBean.class);
      if (addittionCourseBean!= null) {
        try {
//                    if( courseSuperBean.getRows().size()==0)
//                    {
//                        courseSuperAdatper.clear();
//                        lv_course.setEmptyView(empty_view);
//                        lv_course.refreshComplete(0);
//                        return;
//                    }
          if (addittionCourseBean.getRows()!= null && addittionCourseBean.getRows().size() > 0) {
            lv_additioncourse.setVisibility(View.VISIBLE);
            if (firstIndex == 1) {
              additionCourseApt.clear();
              additionCourseApt.setDataList(addittionCourseBean.getRows());
            } else {
              additionCourseApt.addAll(addittionCourseBean.getRows());
            }

            if(addittionCourseBean.getRows().size()<pageSize){
              lv_additioncourse.setNoMore(true);
            }
          } else {
            if (firstIndex == 1) {//第一页
              lv_additioncourse.setEmptyView(empty_view);
              additionCourseApt.clear();
            } else {
              lv_additioncourse.setNoMore(true);
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
        lv_additioncourse.refreshComplete(addittionCourseBean.getRows() == null ? 0 : addittionCourseBean.getRows().size());
        lRecyclerViewAdapter1.notifyDataSetChanged();
      }
    }

    super.onSuccess(result, call, response, what);
  }

  @Override
  public void onUserVisible() {
    lv_additioncourse.refresh();
    requestData();
  }

  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
    return false;
  }
}
